<?php
namespace Modules\Frontend\Controllers;

class UserInfoController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function findFirstByIdAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$service = $this -> account::getInfoServiceInstance() -> findFirstById($serviceUser['user'] -> user_id);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'未认证']);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'Success', 'user_info' => $service['user_info']]);
	}

	public function createAction() {
		$serviceUser = $this -> account::getServiceInstance() -> checkLogin($this -> request -> getQuery('token', 'string', null));
		if($serviceUser['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$serviceUser['code'],'msg'=>$serviceUser['message']]);
		}

		$data = $this -> request -> getPost();
		$data['real_name'] = $data['realName'];
		unset($data['realName']);
		$data['identity_card'] = $data['identityCard'];
		unset($data['identityCard']);
		$data['user_id'] = $serviceUser['user'] -> user_id;

		$service = $this -> account::getInfoServiceInstance() -> create($data);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}

		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'认证成功']);
	}
}